package com.shanghaionstar.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.onstar.cn.weight.CustomImageView;
import com.shanghaionstar.R;

/**
 * Created by 7828wc on 16/7/14.
 */
public class SupernatantLayerLayout extends LinearLayout {
    private View view;
    private CustomImageView layer_image;
    private PopupWindow popupWindow;

    public SupernatantLayerLayout(Context context) {
        super(context);
        initView();
    }

    public SupernatantLayerLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SupernatantLayerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        view = inflate(getContext(), R.layout.common_supernatant_layout, this);
        layer_image = (CustomImageView) view.findViewById(R.id.layer_image);

        layer_image.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
    }

    public void showLayer(View childView, int resId) {
        if (resId != -1) {
            CustomImageView imageView = new CustomImageView(getContext());
            imageView.setImageResource(resId);
            if (popupWindow == null) {
                popupWindow = new PopupWindow(imageView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
            }
//            popupWindow.setContentView(imageView);
//            popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
//            popupWindow.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
            popupWindow.setFocusable(true);
            popupWindow.setAnimationStyle(com.onstar.cn.weight.R.style.share_layout_animation);
//            ColorDrawable dw = new ColorDrawable(0xb0000000);
//            popupWindow.setBackgroundDrawable(dw);
            popupWindow.setOutsideTouchable(true);
            imageView.setAlpha(0.8f);

//            int [] location=new int[2];
//            childView.getLocationOnScreen(location);

            // 设置PopupWindow在View中显示的位置
            popupWindow.showAtLocation(childView, Gravity.CENTER_HORIZONTAL, 0, 0);
//            popupWindow.showAsDropDown(childView);
        }
    }

    public void dismiss() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }
}
